﻿using DevExpress.XtraEditors;
using MDK.Device;
using MDK.Device.Devices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDK.Winform.UserControl.Config
{
    public partial class ATM : BaseForm
    {
        #region 初始化
        public ATM()
        {
            InitializeComponent();
        }

        private void ATM_Load(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() => { RefreshDeviceStatus(); });
        }
        #endregion

        #region 刷新设备状态
        public void RefreshDeviceStatus()
        {
            while (true)
            {
                try
                {
                    Thread.Sleep(1000);
                    if (this.IsHandleCreated)
                    {
                        //ATM电源
                        circle1.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Atm.AtmCamerePower.IsOn())
                                circle1.Set2();
                            else
                                circle1.Set1();
                        }));

                        //相机电源
                        circle2.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Atm.CarmeraPower.IsOn())
                                circle2.Set2();
                            else
                                circle2.Set1();
                        }));
                        //模型
                        circle3.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Atm.Device.IsAutoMode)
                                circle3.Set2();
                            else if (App.flow.SysModel.Atm.Device.IsManualMode)
                                circle3.Set1();
                            else
                                circle3.Set3();
                        }));
                        //急停
                        circle4.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Atm.Device.IsScrammed)
                                circle4.Set2();
                            else
                                circle4.Set1();
                        }));
                        //防夹手传感器
                        circle5.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Atm.DoorAntiPinch.IsActive())
                                circle5.Set2();
                            else
                                circle5.Set1();
                        }));
                        //皮带传感器一
                        circle6.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Atm.BeltStartSensor.IsActive())
                                circle6.Set2();
                            else
                                circle6.Set1();
                        }));
                        //皮带传感器二
                        circle7.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Atm.BeltEndSensor.IsActive())
                                circle7.Set2();
                            else
                                circle7.Set1();
                        }));
                        //推杆传感器
                        circle11.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Atm.CrossingPushrodSensor.IsActive())
                                circle11.Set2();
                            else
                                circle11.Set1();
                        }));
                    }
                }
                catch { }
            }
        }
        #endregion

        #region --详细操作--

        #region ATM电源
        private void btnOpenATM_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.AtmCamerePower.TurnOn();
        }
        private void btnCLoseATM_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.AtmCamerePower.TurnOff();
        }
        #endregion

        #region 相机电源
        private void btnOpenCamera_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.CarmeraPower.TurnOn();
        }

        private void btnCloseCamera_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.CarmeraPower.TurnOff();
        }
        #endregion

        #region 模式切换
        private void btnAuto_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.Device.SwitchAutoMode();
        }

        private void btnMu_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.Device.SwitchManualMode();
        }
        #endregion

        #region 急停

        private void btnScramm_Click(object sender, EventArgs e)
        {
        }

        private void btnNoScramm_Click(object sender, EventArgs e)
        {

        }

        #endregion

        #region 门
        private void btnOpenDoor_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.Door.StartForward();
            //App.flow.SysModel.Atm.Door.WaitCompleted();
        }

        private void btnCloseDoor_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.Door.StartRecede();
            //App.flow.SysModel.Atm.Door.WaitCompleted();
        }
        #endregion

        #region 皮带
        private void btnGo_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.Belt.StartForward();
            App.flow.SysModel.Atm.Belt.WaitCompleted();
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.Belt.StartRecede();
            App.flow.SysModel.Atm.Belt.WaitCompleted();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.Belt.Stop();
            App.flow.SysModel.Atm.Belt.WaitCompleted();
        }
        #endregion

        #region 夹板一
        private void btnOpenDamBoard1_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.CarmeraDamBoard.StartForward();
            App.flow.SysModel.Atm.CarmeraDamBoard.WaitCompleted();
        }

        private void btnCloseDamBoard1_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.CarmeraDamBoard.StartRecede();
            App.flow.SysModel.Atm.CarmeraDamBoard.WaitCompleted();
        }

        private void btnStopDamBoard1_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.CarmeraDamBoard.Stop();
            App.flow.SysModel.Atm.CarmeraDamBoard.WaitCompleted();
        }

        #endregion

        #region 夹板二

        private void btnOpenDamBoard2_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.CrossingDamBoard.StartForward();
            App.flow.SysModel.Atm.CrossingDamBoard.WaitCompleted();
        }

        private void btnCloseDamBoard2_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.CrossingDamBoard.StartRecede();
            App.flow.SysModel.Atm.CrossingDamBoard.WaitCompleted();
        }

        private void btnStopDamBoard2_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.CrossingDamBoard.Stop();
            App.flow.SysModel.Atm.CrossingDamBoard.WaitCompleted();
        }
        #endregion

        #region 推杆

        private void btnGoPushrod_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.CrossingPushrod.StartForward();
            App.flow.SysModel.Atm.CrossingPushrod.WaitCompleted();
        }

        private void btnBackPushrod_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.CrossingPushrod.StartRecede();
            App.flow.SysModel.Atm.CrossingPushrod.WaitCompleted();
        }

        private void btnStopPushrod_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.CrossingPushrod.Stop();
            App.flow.SysModel.Atm.CrossingPushrod.WaitCompleted();
        }
        #endregion

        #region 扫码
        private void btnTrigger_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.FreezerBeltScanner.Trigger();
            App.flow.SysModel.Atm.FreezerBeltScanner.WaitCompleted();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.FreezerBeltScanner.Cancel();
        }
        #endregion

        private void btnOpenScrammed_Click(object sender, EventArgs e)
        {

        }

        private void btnCloseScrammed_Click(object sender, EventArgs e)
        {

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Atm.Device.ClearError();
        }

        #endregion
    }
}
